#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#define INIFITE 0x3f3f3f3f
#define MAX 55
#include <cstring>
typedef long long ll;
using namespace std;
int V[MAX];
int res[MAX];
int visited[MAX];
int e[MAX][MAX];
int n;
void Dijkstra(vector<vector<int> > Edge ) {
    memset(visited,0,sizeof(visited));
    for (int i=0; i<n; i++) res[i]=e[0][i];
    int pre,ma;
    while(1) {
        pre=-1;
        ma=INIFITE;
        for (int i=0; i<n; i++) {
            if (res[i]<ma && !visited[i]) {
                ma=res[i];
                pre=i;
            }
        }
        if (pre<0) break;
        visited[pre]=true;
        for (int i=0; i<Edge[pre].size(); i++) {
            if (e[0][pre] + e[pre][Edge[pre][i]] < e[0][Edge[pre][i]]) {
                e[0][Edge[pre][i]]=e[0][pre] + e[pre][Edge[pre][i]];
                res[Edge[pre][i]]=e[0][Edge[pre][i]];
            }
        }
    }
}
int main() {
    int T;
    cin>>T;
    char ch[55];
    int c;
    while (T--) {
        scanf("%d", &n);
        vector< vector<int> > Edge(n);
        memset(e,0x3f,sizeof(e));
        for (int i=0; i<n; i++) {
            scanf("%s", ch);
            c=0;
            for (int j=0; j<n; j++) {
                if (ch[j]=='Y') {
                    Edge[i].push_back(j);
                    e[i][j]=c;
                    c++;
                }
            }
        }
        for (int i=0; i<n; i++) e[i][i]=0;
        Dijkstra(Edge);
        if (res[n-1]!=INIFITE) {
            printf("%d\n", res[n-1]);
        }
        else printf("-1\n");
    }
    return 0;
}
